<?php


use Biz\model\GoodsTestActivity\FineGoodsMsgModel;
use Biz\model\GoodsTestActivity\FineGoodsWishMsgModel;
use Biz\model\GoodsTestActivity\FineGoodsWishRecordModel;
use Biz\service\GoodsTestActivity\FineGoodsService;
use Biz\cache\brand\BrandShowActivity;


/**
 * Description: 用户许愿
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 14:33
 */
class CreateWishAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;


    public function main()
    {
        $uid = $this->uid;
        if (!$uid) {
            throw new \Exception("请登录重试！");
        }
        $wish_id = $this->getRequest()->getPost('wish_id', 0);
//        $content = $this->getRequest()->getPost('content', '');
        $score = random_int(9, 99);
        $fine_goods_id = $this->getRequest()->getPost('fine_goods_id', 0);
        if (!$wish_id || !$fine_goods_id) {
            throw new \Exception('非法传参');
        }

        $key = "fine_goods_wish_" . $uid;
        $islock = BrandShowActivity::isLock($key, 5);
        if ($islock) {
            throw new \Exception('请不要频繁提交');
        }
        //查询是否重复提交
        $where = ['uid' => $uid, 'fine_goods_id' => $fine_goods_id];
        $wish_record_id = FineGoodsWishRecordModel::first($where, 'id');
        if ($wish_record_id) {
            throw new \Exception('许愿成功了，请不要频繁提交！');
        }

        $data['uid'] = $uid;
        $data['wish_id'] = $wish_id;
        $data['fine_goods_id'] = $fine_goods_id;
        $data['score'] = $score;

        FineGoodsWishRecordModel::insert($data);
        $id = FineGoodsWishRecordModel::id();
        if (!$id) {
            throw new \Exception('提交失败请重试！');
        }
//        $data['content'] = "许愿：" . $content;
        //添加消息记录
        FineGoodsService::addMsgRecord(FineGoodsMsgModel::MSG_WISH, [$data]);

        $this->setSuccess(['score' => $score]);

    }

}